'''
conftest.py文件中的函数不需要导入
位置：项目根目录
查找路径：
    先从当前模块找 -> 当前目录 -> 往上级节点查找

'''
import time
import pytest
from pythoncode.calculator import Calculator


def pytest_collection_modifyitems(items):
    '''
    测试用例收集完成时，将收集的item的name和nodeid的中文显示转码
    :param items:
    :return:
    '''
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode_escape')
        item._nodeid = item._nodeid.encode('utf-8').decode('unicode_escape')

@pytest.fixture(autouse=True)
def print_function():
    print('开始测试')
    yield
    print('结束测试')

@pytest.fixture(scope='session', autouse=True)
def calc():
    calc = Calculator()
    yield calc
    print('测试结束')


@pytest.fixture(scope="session", autouse=True)
def manage_logs(request):
    """Set log file name same as test name"""
    now = time.strftime("%Y-%m-%d %H-%M-%S")
    log_name = 'output/log/' + now + '.logs'

    request.config.pluginmanager.get_plugin("logging-plugin").set_log_path(log_name)
